﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace QServices
{
    public static class MD5Helper
    {
        /// <summary>
        /// 获取字符串MD5值
        /// </summary>
        /// <param name="sText"></param>
        /// <param name="encoding">编码方式</param>
        /// <param name="uppercase">true 大写；false 小写</param>
        /// <returns></returns>
        public static string Encrypt(string sText, Encoding encoding, bool uppercase = true)
        {
            byte[] buffer = encoding.GetBytes(sText);
            string s = Encrypt(buffer);
            if (!uppercase)
                s = s.ToLower();
            return s;
        }
        /// <summary>
        /// 获取字符串MD5值
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static string Encrypt(byte[] buffer)
        {
            //使用MD5这个抽象类的Creat()方法创建一个虚拟的MD5类的对象。
            using (MD5 md5 = MD5.Create())
            {
                //使用MD5实例的ComputerHash()方法处理字节数组。
                byte[] bufferNew = md5.ComputeHash(buffer);

                string s = BitConverter.ToString(bufferNew, 0, bufferNew.Length);
                return s.Replace("-", "");
            }
        }
    }
}
